﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.company
{
    public partial class companyMoneyListBody : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();

       public string ParamentType
        {
            get {
                return Request.QueryString["type"];
            }
        }


        public void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ParamentType == "publish")
                {
                 
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.PublishCompanyMoneyList);
                }
                else  if (base.CompanyId == null )
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.CompanyMoneyList);
                }
                this.BindGrid();
            }
        }

        private void BindGrid()
        {

            Grid1.RecordCount = this.theCompany.GetRecordCount(GetSqlWhere);
            string strOrderBy = string.Format("{0} {1} "
                                               , ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                               , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
            if (strOrderBy.Length < 13)
            {
                strOrderBy = "CompanyCode desc ";
            }
            DataTable table = this.theCompany.GetCompanyMoneyListByPage(GetSqlWhere, strOrderBy, Grid1.PageIndex, Grid1.PageSize).Tables[0];

            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };
        }



        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        string GetSqlWhere
        {
            get
            {
                StringBuilder sbWhere = new StringBuilder();
                sbWhere.AppendFormat("  (parentId = {0} or id={0} )", CompanyParamId);


                if (this.txtKeywords.Text.Trim() != "")
                {
                    sbWhere.AppendFormat(" and (Name like '%{0}%' or CompanyCode = '{0}' or CompanyShortName like '%{0}%')", this.txtKeywords.Text.Trim());
                }

                return sbWhere.ToString();
            }
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = theCompany.GetCompanyMoneyListByPage(GetSqlWhere, string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            ViewState["sort"] = e.SortField;
            ViewState["dir"] = e.SortDirection;
        }


        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.BindGrid();
        }

    


        string CompanyParamId
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    return Request.QueryString["id"];
                }
                else
                {
                    if (string.IsNullOrEmpty(base.CompanyId))
                    {
                        return "0";
                    }
                    else
                    {
                        return base.CompanyId;
                    }
                }
            }
        }

        public string HeaderHtml
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"] != "0")
                {

                    tbCompany oCompany = theCompany.GetModel(int.Parse(Request.QueryString["id"]));

                    string sHead = string.Format(" <div  class=\"navgater\"><img src=\"../images/module.gif\" width=\"20\" height=\"22\" alt=\"\"/> "
                                              + " 公司名称：{0} "
                                              + " <a style=\" display:block; float:right;\" href=\"javascript:location.href='companyMoneyListBody.aspx?id={1}&type={2}';\" ><img src=\"../images/history.gif\" border=\"0\" /></a>"
                                              + " </div>"
                                              , oCompany.Name
                                              , oCompany.ParentId.ToString()
                                              , ParamentType);
                    return sHead;
                }
                return string.Empty;

            }


        }

        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
            if (Grid1.DataKeys[e.RowIndex][0] == base.CompanyId || !base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyMoney))
            {
                Grid1.Rows[e.RowIndex].FindControl("divInMoney").Visible = false;
                Grid1.Rows[e.RowIndex].FindControl("divOutMoney").Visible = false;
            }
            //if ((e.DataItem.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            //{
            //    if (e.Row.RowType == DataControlRowType.DataRow)
            //    {
            //        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
            //        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");

            //        if (GridView1.DataKeys[e.Row.RowIndex].Value.ToString() == base.CompanyId || !base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyMoney))
            //        {
            //            e.Row.Cells[11].Controls[1].Visible = false;
            //            e.Row.Cells[11].Controls[3].Visible = false;
            //        }
            //    }
            //}
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void btnBrachDeleted_Click(object sender, EventArgs e)
        {
            //string sIds = "";
            //for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            //{
            //    CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
            //    if (box.Checked)
            //    {
            //        sIds = sIds + this.GridView1.DataKeys[i].Value + ",";
            //    }
            //}
            //if (sIds.Length > 0)
            //{
            //    sIds = sIds.Remove(sIds.Length - 1);

            //    theCompany.DeleteMany(sIds);
            //    base.Response.Write("<script>alert('批量删除成功!')</script>");
            //    this.BindGridView(string.Empty);
            //}

            //    }

        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindGrid();
        }

    }
}